﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DevExpress.MailClient.Win.Controls.Helper;
using DevExpress.XtraEditors;
using DevExpress.XtraGauges.Win.Gauges.State;
using Zero.POCO.Compagne;
using Zero.POCO.Donnees;
using Zero.POCO.Views;

namespace DevExpress.MailClient.Win.Controls.ProfilPenetration {
    public partial class WidgetReglages : UserControl {
        GridCheckMarksSelection gridCheckMarksSA;
        private GridCheckMarksSelection gridCheckMarksVagueSA;
        Timer updateTimer = new Timer();
        static Color positiveColor = Color.FromArgb(70, 158, 165);
        static Color negativeColor = Color.FromArgb(165, 70, 113);

        private List<SupportsView> listeSupportsViewsRef;
        private List<Vague> listeVaguesViewsRef;
        private List<SupportsView> _supports;
        private List<Vague> _vagues;
        public event EventHandler<EventArgsProfilPenetration> ChangeProfilPentrationEvent;
        public WidgetReglages(List<Vague> vagues, List<SupportsView> supports)
        {
            InitializeComponent();
            _supports = supports;
            _vagues = vagues;
            cbMEdia.Properties.Items.Add("TV");
            cbMEdia.Properties.Items.Add("RD");
            cbMEdia.Properties.Items.Add("PR");
            cbMEdia.SelectedIndex = 0;

            cbDistrib.Properties.Items.Add("+1");
            cbDistrib.Properties.Items.Add("+2");
            cbDistrib.Properties.Items.Add("+3");
            cbDistrib.Properties.Items.Add("+4");
            cbDistrib.Properties.Items.Add("+5");
            cbDistrib.Properties.Items.Add("+6");
            cbDistrib.SelectedIndex = 0;

            luSupport.Properties.DataSource=(supports.Where(e=>e.Media==TypeMedia.TV).Select(e=>e.Nom).ToList());
            luSupport.ItemIndex = 0;

            gluSupportRef.Properties.DataSource = (supports.Where(e => e.Media == TypeMedia.TV).Select(e => e.Nom).ToList());
            gluSupportRef.Properties.DisplayMember = "Nom";

            var listeVague = vagues.Select(e => e.Libelle);
            luVague.Properties.DataSource = listeVague;
            luVague.ItemIndex = 0;


            gluVagueRef.Properties.DataSource = vagues;
            gluVagueRef.Properties.DisplayMember = "Libelle";


            gluVagueRef.Properties.View.OptionsSelection.MultiSelect = true;
            gluVagueRef.CustomDisplayText += gluVagueRef_CustomDisplayText;
            gluVagueRef.Properties.PopulateViewColumns();

            gridCheckMarksVagueSA = new GridCheckMarksSelection(gluVagueRef.Properties);

            gridCheckMarksVagueSA.SelectionChanged += gridCheckMarksVague_SelectionChanged;

            gluVagueRef.Properties.Tag = gridCheckMarksVagueSA;




            gluSupportRef.Properties.View.OptionsSelection.MultiSelect = true;
            gluSupportRef.CustomDisplayText += gluSupportsRef_CustomDisplayText;
            gluSupportRef.Properties.PopulateViewColumns();

            gridCheckMarksSA = new GridCheckMarksSelection(gluSupportRef.Properties);

            gridCheckMarksSA.SelectionChanged += gridCheckMarks_SelectionChanged;
            gluSupportRef.Properties.Tag = gridCheckMarksSA;




            updateTimer.Interval = 1000;
            updateTimer.Tick += OnTimerTick;updateTimer.Start();
            OnTimerTick(this, EventArgs.Empty);



        }


        void gridCheckMarks_SelectionChanged(object sender, EventArgs e)
        {
            if (ActiveControl is GridLookUpEdit)
            {
                int i = 0;
                StringBuilder sb = new StringBuilder();
                foreach (string rv in (sender as GridCheckMarksSelection).Selection)
                {
                    if (sb.ToString().Length > 0) { sb.Append(", "); }
                    sb.Append(rv);

                }
                (ActiveControl as GridLookUpEdit).Text = sb.ToString();

            }
        }

        private void gluSupportsRef_CustomDisplayText(object sender, XtraEditors.Controls.CustomDisplayTextEventArgs e)
        {
            StringBuilder sb = new StringBuilder();
            GridCheckMarksSelection gridCheckMark = (sender as GridLookUpEdit).Properties.Tag as GridCheckMarksSelection;
            if (gridCheckMark == null) return;
            listeSupportsViewsRef = new List<SupportsView>();
            foreach (string rv in gridCheckMark.Selection)
            {
                if (sb.ToString().Length > 0) { sb.Append(", "); }
                sb.Append(rv);



                listeSupportsViewsRef.Add(_supports.First(sup => sup.Nom == rv));
            }
            e.DisplayText = sb.ToString();
        }




        private void gluVagueRef_CustomDisplayText(object sender, XtraEditors.Controls.CustomDisplayTextEventArgs e)
        {
            StringBuilder sb = new StringBuilder();
            GridCheckMarksSelection gridCheckMark = (sender as GridLookUpEdit).Properties.Tag as GridCheckMarksSelection;
            if (gridCheckMark == null) return;
            listeVaguesViewsRef = new List<Vague>();
            foreach (Vague rv in gridCheckMark.Selection)
            {
                if (sb.ToString().Length > 0) { sb.Append(", "); }
                sb.Append(rv.Libelle);
                listeVaguesViewsRef.Add(rv);
            }
            e.DisplayText = sb.ToString();
        }


        void gridCheckMarksVague_SelectionChanged(object sender, EventArgs e)
        {
            if (ActiveControl is GridLookUpEdit)
            {
                int i = 0;
                StringBuilder sb = new StringBuilder();
                foreach (Vague rv in (sender as GridCheckMarksSelection).Selection)
                {
                    if (sb.ToString().Length > 0) { sb.Append(", "); }
                    sb.Append(rv.Libelle);

                }
                (ActiveControl as GridLookUpEdit).Text = sb.ToString();

            }
        }

        int index = 0;
       // StockData previousData;
        void OnTimerTick(object sender, EventArgs e) {
         
        }
        private void OnCustomDrawElement(object sender, DevExpress.XtraGauges.Core.Primitive.CustomDrawElementEventArgs e) {
            e.Handled = true;
            StateIndicatorComponent indicator = sender as StateIndicatorComponent;
            Point p1 = Point.Empty;
            Point p2 = Point.Empty;
            Point p3 = Point.Empty;
            int height = (int)(e.Info.BoundBox.Width * Math.Sqrt(3) / 2);
            p1.X = (int)(e.Info.BoundBox.Width / 2);
            p2.X = (int)e.Info.BoundBox.Left;
            p3.X = (int)e.Info.BoundBox.Right;
            int y = (int)((e.Info.BoundBox.Height - height) / 2);
            Color stateColor = Color.Empty;
            if(indicator.StateIndex == 0) {
                p1.Y = y;
                p2.Y = p3.Y = y +height;
                stateColor = positiveColor;
            }
            else {
                p1.Y = y + height;
                p2.Y = p3.Y = y;
                stateColor = negativeColor;
            }
            e.Context.Graphics.FillPolygon(new SolidBrush(stateColor), new Point[] { p1, p2, p3 });
        }

        public void btnActualiser_Click(object sender, EventArgs e)
        {

            var eventProfil = new EventArgsProfilPenetration();
            eventProfil.Support = (SupportsView) _supports.First(sup => sup.Nom == luSupport.EditValue);
            eventProfil.Vague = (Vague) _vagues.First(vag => vag.Libelle == luVague.EditValue);
            eventProfil.SupportRef = listeSupportsViewsRef;
            eventProfil.VagueRef = listeVaguesViewsRef;
            eventProfil.Distribution = cbDistrib.SelectedIndex + 1;
           
            var handlerProfile = ChangeProfilPentrationEvent;
            if (handlerProfile != null)
                handlerProfile(this, eventProfil);

        }

        private void panel2_Paint(object sender, PaintEventArgs e)
        {

        }


        public class EventArgsProfilPenetration : EventArgs
        {
            public SupportsView Support { get; set; }
            public List<SupportsView> SupportRef { get; set; }


            public Vague Vague { get; set; }
            public List<Vague> VagueRef { get; set; }
            public int Distribution { get; set; }
        }

        private void gridLookUpEdit2_EditValueChanged(object sender, EventArgs e)
        {

        }


    }
}
